﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameName1
{
    class SpaceShip : GameObject, IHittable, IDisplayable, IMovable
    {
        
        public int Frame { get; set; }
        public double ReloadTime { get { return Weapon.ReloadTime; } set { Weapon.ReloadTime = value; } }

        public Weapon Weapon { private get; set; }

        public SpaceShipState SpaceShipState {private get; set; }

        public SpaceShip(Vector2 position) : base(position)
        {
           SpaceShipState = new SpaceShipStateNormal();
        }

        public override void Update(GameTime gameTime)
        {
            SpaceShipState.Update(gameTime,this);
        }
        public static void LoadContent(ContentManager contentManager)
        {
            _commonSprite = contentManager.Load<Texture2D>("Sprites/space_ship_sheet2.png");
        }
        public void GetInput()
        {
            KeyboardState keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Left) && Position.X > 10)
            {
                Velocity = new Vector2(-6, 0);
                Move();
            }

            if (keyState.IsKeyDown(Keys.Right) && Position.X < ScreenManager.GetInstance().Resolution.X - 10 - GetDestinationRectangle().Width)
            {
                Velocity = new Vector2(6, 0);
                Move();
            }

            if (keyState.IsKeyDown(Keys.Space))
                Shoot();
        }
        private void Shoot()
        {
            var spawnPoint = new Vector2(Position.X + GetDestinationRectangle().Width / 2, Position.Y - 15);
            Weapon.Fire<Rocket>(spawnPoint);
        }

        public void HandleHit(IHittable other, GameTime colissionTime)
        {
            SpaceShipState.HandleHit(other,colissionTime,this);
        }
        public Rectangle GetCollisionBox()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, Sprite.Width / 2, Sprite.Height);
        }
        public IEnumerable<Type> GetNotHittableTypes()
        {
            return new List<Type>{typeof(PowerUpHealth), typeof(PowerUpRapidFire)};
        }

        public int DrawingOrder { get { return 0; } }
        public void Draw(SpriteBatch spriteBatch)
        {
            SpaceShipState.Draw(spriteBatch, this);
        }
        private static Texture2D _commonSprite;
        public Texture2D Sprite { get { return _commonSprite; } }
        public Rectangle GetSourceRectangle()
        {
            return new Rectangle((Sprite.Width / 2) * (Frame % 2), 0, Sprite.Width / 2, Sprite.Height);         
        }
        public Rectangle GetDestinationRectangle()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, Sprite.Width/2, Sprite.Height);
        }

        public Vector2 Velocity { get; set; }
        public void Move()
        {
            Position += Velocity;
        }
    }
}
